package com.sskj.wallet.ui.activity;


import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.CoinEnum;
import com.sskj.lib.bean.WalletBean;
import com.sskj.lib.model.room.WalletBeanViewModel;
import com.sskj.lib.util.CopyUtil;
import com.sskj.wallet.R;
import com.sskj.wallet.R2;
import com.sskj.wallet.bean.WalletCardBean;
import com.sskj.wallet.component.DaggerUserDataComponent;
import com.sskj.wallet.presenter.BackupKeystoreActivityPresenter;
import com.sskj.wallet.uitl.TipWalletUtil;

import java.util.ArrayList;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

@Route(path = RConfig.WALLET_BACKUP_KEYSTORE)//备份Keystore
public class BackupKeystoreActivity extends BaseActivity<BackupKeystoreActivityPresenter> {
    @Autowired
    boolean isPrivate;
    @BindView(R2.id.tvDesc)
    TextView tvDesc;
    @BindView(R2.id.tvContent)
    TextView tvContent;
    @BindView(R2.id.btBackup)
    Button btBackup;
    @BindView(R2.id.btShowQR)
    Button btShowQR;
    @Autowired
    String code;
    @Inject
    WalletBeanViewModel walletBeanViewModel;
    private WalletBean walletBeanBtc;
    private WalletBean walletBeanEth;

    @Override
    protected int getLayoutId() {
        return R.layout.wallet_activity_backup_keystore;
    }

    @Override
    public BackupKeystoreActivityPresenter getPresenter() {
        return new BackupKeystoreActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        DaggerUserDataComponent.create().inject(this);
        if (isPrivate) {
            setTitle(App.INSTANCE.getString(R.string.wallet_backupKeystoreActivity1_j));
            tvDesc.setText(App.INSTANCE.getString(R.string.wallet_backupKeystoreActivity2_j));
            btShowQR.setText(App.INSTANCE.getString(R.string.wallet_backupKeystoreActivity3_j));
        } else {
            setTitle(App.INSTANCE.getString(R.string.wallet_backupKeystoreActivity4_j));
        }

        walletBeanViewModel.getUsers().observe(this, walletBeans -> {
            if (walletBeans != null) {
                if (walletBeans.get(0).getCode().equals(CoinEnum.BTC.getCode())) {
                    walletBeanBtc = walletBeans.get(0);
                    walletBeanEth = walletBeans.get(1);
                } else {
                    walletBeanBtc = walletBeans.get(1);
                    walletBeanEth = walletBeans.get(0);
                }


                if (isPrivate) {
                    if (code.equals(CoinEnum.BTC.getCode())) {

                        tvContent.setText(walletBeanBtc.getPriDe());
                    } else {
                        tvContent.setText(walletBeanEth.getPriDe());
                    }

                    ClickUtil.click(btShowQR, () -> {
                        if (code.equals(CoinEnum.BTC.getCode())) {
                            TipWalletUtil.showQRCode(this, walletBeanBtc.getPriDe());
                        } else {
                            TipWalletUtil.showQRCode(this, walletBeanEth.getPriDe());

                        }
                    });
                } else {
                    if (code.equals(CoinEnum.BTC.getCode())) {

                        tvContent.setText(walletBeanBtc.getKeystore());
                    } else {
                        tvContent.setText(walletBeanEth.getKeystore());
                    }
                    ClickUtil.click(btShowQR, () -> {
                        if (code.equals(CoinEnum.BTC.getCode())) {
                            TipWalletUtil.showQRCode(this, walletBeanBtc.getKeystore());
                        } else {
                            TipWalletUtil.showQRCode(this, walletBeanEth.getKeystore());

                        }
                    });
                }
                ClickUtil.click(btBackup, () -> {
                    CopyUtil.copy(tvContent.getText().toString());
                    if (code.equals(CoinEnum.BTC.getCode())) {
                        walletBeanBtc.setBackup(true);
                        walletBeanViewModel.insert(walletBeanBtc);
                    } else {
                        walletBeanEth.setBackup(true);
                        walletBeanViewModel.insert(walletBeanEth);
                    }
                });

            }
        });

    }


}
